﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using GreyGate.Kinect;
using GreyGate.Objects;
using GreyGate.Utils;

namespace GreyGate.Screens
{
    class MenuScreen
    {
        private ContentManager content;
        private GraphicsDeviceManager graphics;
        private KinectMoveDetection kinectUtils;

        private Texture2D dividingLine;
        private Viewport defaultViewport;

        private Player player1;
        private Player player2;

        private Labyrinth labyrinth;

        public MenuScreen(ContentManager content, GraphicsDeviceManager graphics,
                          KinectMoveDetection kinectUtils)
        {
           this.content = content;
           this.graphics = graphics;
           this.kinectUtils = kinectUtils;

           initialize();
        }

        private void initialize()
        {
            labyrinth = new Labyrinth(content, 5, false);

            defaultViewport = graphics.GraphicsDevice.Viewport;
            dividingLine = content.Load<Texture2D>("Textures\\trennlinie");

            #region player property settings
            Viewport player1Viewport = defaultViewport;
            player1Viewport = new Viewport();
            player1Viewport.X = 0;
            player1Viewport.Y = 0;
            player1Viewport.Width = defaultViewport.Width / 2;
            player1Viewport.Height = defaultViewport.Height;
            player1Viewport.MinDepth = 0;
            player1Viewport.MaxDepth = 1;

            player1 = new Player(graphics, content, player1Viewport,
                                 new int[] { (int)labyrinth.Entrance.getPosition.Y, (int)labyrinth.Entrance.getPosition.X },
                                 UtilityVariables.labyrinthSize);

            Viewport player2Viewport = defaultViewport;
            player2Viewport = new Viewport();
            player2Viewport.X = defaultViewport.Width / 2;
            player2Viewport.Y = 0;
            player2Viewport.Width = defaultViewport.Width / 2;
            player2Viewport.Height = defaultViewport.Height;
            player2Viewport.MinDepth = 0;
            player2Viewport.MaxDepth = 1;

            player2 = new Player(graphics, content, player2Viewport,
                                 new int[] { (int)labyrinth.Exit.getPosition.Y, (int)labyrinth.Exit.getPosition.X },
                                 UtilityVariables.labyrinthSize);

            player1.setOpponent(ref player2);
            player2.setOpponent(ref player1);
            #endregion
        }

        public void update(GameTime gameTime)
        {
            kinectUtils.updateMenu();

            #region player1Actions
            switch (kinectUtils.player1Gesture)
            {
                case Gesture.FORWARD:
                    if (!player1.MovesBack)
                    {
                        player1.MovesForward = true;
                    }
                    break;
                case Gesture.BACK:
                    if (!player1.MovesForward)
                    {
                        player1.MovesBack = true;
                    }
                    break;
                case Gesture.ROTATELEFT:
                    // die if-Abfragen in diesen Fällen sind dazu gedacht einen
                    // Deadlock zu verhindern, der entstehen kann, wenn links
                    // und rechts gleichzeitig aktiviert wird.
                    if (!player1.TurnsAround && !player1.RotatesRight && !player1.MovesBack && !player1.MovesForward)
                    {
                        player1.RotatesLeft = true;
                    }
                    break;
                case Gesture.ROTATERIGHT:
                    if (!player1.TurnsAround && !player1.RotatesLeft && !player1.MovesBack && !player1.MovesForward)
                    {
                        player1.RotatesRight = true;
                    }
                    break;
                case Gesture.TURNAROUND:
                    if (!player1.RotatesRight && !player1.RotatesLeft && !player1.MovesBack && !player1.MovesForward)
                    {
                        player1.TurnsAround = true;
                    }
                    break;
                case Gesture.NOGES:
                    //Console.WriteLine("NOGES");
                    break;

            }
            #endregion

            #region player2Actions
            switch (kinectUtils.player2Gesture)
            {
                case Gesture.FORWARD:
                    if (!player2.MovesBack)
                    {
                        player2.MovesForward = true;
                    }
                    break;
                case Gesture.BACK:
                    if (!player2.MovesForward)
                    {
                        player2.MovesBack = true;
                    }
                    break;
                case Gesture.ROTATELEFT:
                    // die if-Abfragen in diesen Fällen sind dazu gedacht einen
                    // Deadlock zu verhindern, der entstehen kann, wenn links
                    // und rechts gleichzeitig aktiviert wird.
                    if (!player2.TurnsAround && !player2.RotatesRight && !player2.MovesBack && !player2.MovesForward)
                    {
                        player2.RotatesLeft = true;
                    }
                    break;
                case Gesture.ROTATERIGHT:
                    if (!player2.TurnsAround && !player2.RotatesLeft && !player2.MovesBack && !player2.MovesForward)
                    {
                        player2.RotatesRight = true;
                    }
                    break;
                case Gesture.TURNAROUND:
                    if (!player2.RotatesRight && !player2.RotatesLeft && !player2.MovesBack && !player2.MovesForward)
                    {
                        player2.TurnsAround = true;
                    }
                    break;
                case Gesture.NOGES:
                    //Console.WriteLine("NOGES");
                    break;

            }
            #endregion

            player1.update(labyrinth.getLabyrinth[player1.CurrentPos[0], player1.CurrentPos[1]]);
            player2.update(labyrinth.getLabyrinth[player2.CurrentPos[0], player2.CurrentPos[1]]);

            player1.checkForKeyboard();
        }

        public void draw(SpriteBatch spriteBatch)
        {
            //if (kinectUtils.kinectRGBVideo != null)
            //{    
            //    spriteBatch.Begin();
            //    spriteBatch.Draw(kinectUtils.kinectRGBVideo, 
            //                     new Rectangle(0, 0, 
            //                                        graphics.PreferredBackBufferWidth, 
            //                                        graphics.PreferredBackBufferHeight), 
            //                     Color.White);
            //    spriteBatch.End();
            //}

            #region labyrinth-player-screen Drawing
            graphics.GraphicsDevice.Viewport = player1.View;

            player1.draw(labyrinth.getLabyrinth);
            player2.draw(labyrinth.getLabyrinth);

            // male eine Trennlinie genau in die Mitte der beiden Screens
            graphics.GraphicsDevice.Viewport = defaultViewport;

            spriteBatch.Begin();
            spriteBatch.Draw(dividingLine,
                              new Rectangle(defaultViewport.Width / 2, 0,
                                             20, defaultViewport.Height),
                               new Color(255, 255, 255, 255));
            spriteBatch.End();
            graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            #endregion  
        }

    }
}
